#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 3e4 + 10;

int p[N], d[N], cnt[N];

int find(int x){
    if(x != p[x]){
        int t = find(p[x]);
        d[x] += d[p[x]];
        p[x] = t;
    }
    return p[x];
}

void merge(int a, int b){
    int fa = find(a), fb = find(b);
    if(fa == fb) return;

    d[fa] = cnt[fb];
    p[fa] = fb;

    cnt[fb] += cnt[fa];
    cnt[fa] = 0;
}

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    for(int i = 1; i < N; i ++) p[i] = i, d[i] = 0, cnt[i] = 1;
    int T;
    cin >> T;

    char op[2];
    int a, b;
    while(T--){
        cin >> op;
        if(*op == 'M'){
            cin >> a >> b;
            merge(a, b);

        }else{
            cin >> a >> b;
            if(a == b) {
                cout << 0 << '\n';
                continue;
            }
            int fa = find(a), fb = find(b);
            if(fa != fb) cout << -1 << '\n';
            else {
                // cout << d[a] << " " << d[b] << '\n';
                
                cout << max(d[a], d[b]) - min(d[a], d[b]) - 1 << '\n';
            }
        }
    }

    return 0;
}